<?php
    class Zendvn_Acl_AclPlugin extends Zend_Controller_Plugin_Abstract
    {
        protected $_acl;
        
        public function __construct($acl){
            $this->_acl = $acl;
        }
        
        public function preDispatch(Zend_Controller_Request_Abstract $res){
            $auth = Zend_Auth::getInstance();
            if($auth->hasIdentity()){
                $infoUser = $auth->getIdentity();
                $level    = $infoUser->role;
                $role     = '';
                switch($level){
                    case    1   :   $role = 'admin'; break;
                    case    2   :   $role = 'smod'; break;
                    case    3   :   $role = 'mod'; break;
                    default     :   $role = 'guest'; break;
                }
                $module     = $res->getModuleName();
                $controller = $res->getControllerName();
                $action     = $res->getActionName();
                if(!$this->_acl->isAllowed($role, $module . ':' . $controller, $action)){
                    $res->setModuleName('error')
                        ->setControllerName('error')
                        ->setActionName('msg');
                }
            }
        }
    }
